package com.lswsjw.android.lib.sqlite;

import java.util.Hashtable;
import java.util.Locale;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

/**
 * @author limtaekyu Sqlite DB Manager
 */
public class DBManager
{
    public static final int FROM_FILE = 0;
    
    public static final int NEW_DB = 1;
    
    private DBOpenHelper mDBOpenHelper;
    
    private SQLiteDatabase mDatabase;
    
    private Context mContext;
    
    private String mPackageName;
    
    private String mDbName;
    
    private int mDbVersion;
    
    /**
     * Sqlite DB Manager. you must make DBOpenFromFile or DBOpenFromSql and set
     * them using setDbOpenHelper of this class.
     * 
     * @param context
     * @param dbName
     * @param dbVersion
     * @param packageName
     */
    public DBManager(Context context, String dbName, int dbVersion,
            String packageName)
    {
        this.mContext = context;
        this.mDbName = dbName;
        this.mDbVersion = dbVersion;
        this.mPackageName = packageName;
    }
    
    public void setDbOpenHelper(DBOpen dbOpen)
    {
        dbOpen.setParams(this.mContext, this.mDbName, null,
                                this.mDbVersion, this.mPackageName);
        this.mDBOpenHelper = dbOpen.getDBOpen();
        
        try
        {
            mDatabase = mDBOpenHelper.getDataBase(DBOpenHelper.READ_WRITE);
            mDatabase.setLocale(Locale.getDefault());
            mDatabase.setLockingEnabled(true);
            // mDatabase.setVersion(0);
        }
        catch (SQLiteException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (WrongParameterException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void closeDB()
    {
        this.mDBOpenHelper.close();
    }
    
    public void execSQL(String sql)
    {
        mDatabase.execSQL(sql);
    }
    
    public void insertTable(String tableName, Hashtable<String, String> map)
    {
        ContentValues values = new ContentValues();
        Set<String> keys = map.keySet();
        
        for (String key : keys)
        {
            values.put(key, map.get(key));
        }
        
        long result = mDatabase.insert(tableName, null, values);
        Log.i("testsql", "" + result);
    }
    
    public void updateTable(String tableName, Hashtable<String, String> map,
            String condition, String[] conditionValues)
    {
        ContentValues values = new ContentValues();
        Set<String> keys = map.keySet();
        
        for (String key : keys)
        {
            values.put(key, map.get(key));
        }
        
        long result = mDatabase.update(tableName, values, condition,
                                       conditionValues);
        Log.i("testsql", "" + result);
    }
    
    public void deleteTable(String tableName, String condition,
            String[] conditionValues)
    {
        long result = mDatabase.delete(tableName, condition, conditionValues);
        Log.i("testsql", "" + result);
    }
    
    /**
     * 
     * @param tableName
     * @param columns
     * @param condition
     * @param conditionValues
     * @param groupBy
     * @param having
     * @param orderBy
     * @return
     */
    public Cursor selectTable(String tableName, String[] columns,
            String condition, String[] conditionValues, String groupBy,
            String having, String orderBy)
    {
        Cursor c = mDatabase.query(tableName, columns, condition,
                                   conditionValues, groupBy, having, orderBy);
        
        Log.i("testsql", "get columnCount : " + c.getColumnCount()
                + ", getRowCount():" + "" + c.getCount());
        for (int i = 0; i < c.getColumnCount(); i++)
        {
            Log.i("testsql", c.getColumnName(i));
        }
        
        c.moveToFirst();
        while (!(c.isAfterLast()))
        {
            String str = "";
            for (int i = 0; i < c.getColumnCount(); i++)
            {
                str += " " + c.getString(i);
            }
            Log.i("testsql", str);
            c.moveToNext();
        }
        
        return c;
    }
    
}
